package com.kch.web.controller.system;

import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.common.json.JSON;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.OtherConfig;
import com.kch.system.service.IOtherConfigService;
import com.kch.system.vo.UnionVO;
import com.kch.web.config.CloumnProperties;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/system/union")
@Api(tags = "领域联盟")
public class UnionController extends BaseController {

    private String prefix = "system/union";

    @Autowired
    private IOtherConfigService otherConfigService;
    @Autowired
    private CloumnProperties cloumnProperties;

    @RequiresPermissions("system:union:view")
    @GetMapping()
    public String index(ModelMap mmap) {
        return prefix + "/union";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:union:list")
    @ResponseBody
    public TableDataInfo list(OtherConfig config) throws Exception {
        List<String> list = new ArrayList<>();
        list.add(cloumnProperties.getUnion());
        startPage();
        List<OtherConfig> configList = otherConfigService.selectByCloCodeList(list, config);
        List<UnionVO> unionVOList = new ArrayList<>();
        for (OtherConfig otherConfig : configList) {
            UnionVO unmarshal = JSON.unmarshal(otherConfig.getContent(), UnionVO.class);
            unmarshal.setId(otherConfig.getId());
            unionVOList.add(unmarshal);
        }
        return getDataTable(unionVOList);
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    @Log(title = "领域联盟", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated UnionVO unionVO) throws Exception {
        OtherConfig config = new OtherConfig();
        config.setCreateTime(new Date());
        config.setCreateBy(ShiroUtils.getLoginName());
        config.setCloumnCode(cloumnProperties.getUnion());
        config.setTitle(unionVO.getName());
        config.setContent(JSON.marshal(unionVO));
        otherConfigService.insertOtherConfig(config);
        return toAjax(true);
    }

    @Log(title = "领域联盟", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(otherConfigService.deleteConfig(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) throws Exception {
        OtherConfig config = otherConfigService.selectById(id);
        UnionVO unionVO = JSON.unmarshal(config.getContent(), UnionVO.class);
        unionVO.setId(config.getId());
        mmap.put("union", unionVO);
        return prefix + "/edit";
    }

    @Log(title = "会议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated UnionVO unionVO) {
        try {
            OtherConfig config = otherConfigService.selectById(unionVO.getId());
            config.setUpdateTime(new Date());
            config.setUpdateBy(ShiroUtils.getLoginName());
            config.setContent(JSON.marshal(unionVO));
            config.setId(unionVO.getId());
            config.setTitle(unionVO.getName());
            config.setCloumnCode(cloumnProperties.getUnion());
            otherConfigService.updateById(config);
            return toAjax(true);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

}
